//
//  CCHomeViewController.swift
//  SinaWebo
//
//  Created by 陶建 on 16/3/24.
//  Copyright © 2016年 taoJ. All rights reserved.
//

import UIKit

class CCHomeTableViewController: CCBasicViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if !isLoginFlag {
            customView.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", title: "关注一些人, 回这里看看有什么惊喜")
            
            navigationItem.leftBarButtonItem = registerLeftBarButtonItem
            navigationItem.rightBarButtonItem = loginRightBarButtonItem
            return
        }
        navigationItem.leftBarButtonItem = friendFocusLeftBarButtonItem
        navigationItem.rightBarButtonItem = radarOrScanRightBarButtonItem
        navigationItem.titleView = titleView
    }

    private lazy var titleView: CCTitleButton = {
        let titleView = CCTitleButton()
        titleView.setTitleColor(UIColor.blackColor(), forState: .Normal)
        titleView.setBackgroundImage(UIImage(named:"actionsheet_cancelbutton_background"), forState: .Highlighted)
        titleView.setTitle("陶建 ", forState: .Normal)
        titleView.setImage(UIImage(named: "navigationbar_arrow_down"), forState: .Normal)
        titleView.setImage(UIImage(named: "navigationbar_arrow_down"), forState: .Highlighted)
        
        titleView.setImage(UIImage(named: "navigationbar_arrow_up"), forState: .Selected)
        titleView.addTarget(self, action: #selector(CCHomeTableViewController.tapTitleView(_:)), forControlEvents: .TouchUpInside)
        titleView.sizeToFit()
        
        return titleView
    }()
    
    func tapTitleView(button: UIButton) {
        let popVC = CCPopViewController(nibName: CCPopViewController.className, bundle: nil)
        popVC.delegate = self
        // 设置专场动画delegate
        popVC.transitioningDelegate = popoverAnimation
        // 设置专场动画类型
        popVC.modalPresentationStyle = .Custom
        presentViewController(popVC, animated: true, completion: nil)
    }
    
    private lazy var registerLeftBarButtonItem: UIBarButtonItem = {
        let registerLeftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: #selector(CCHomeTableViewController.tapRegisterItem))
        
        return registerLeftBarButtonItem
    }()
    
    func tapRegisterItem() {
        print("tapRegisterItem")
    }
    
    private lazy var friendFocusLeftBarButtonItem: UIBarButtonItem = {
        let loginRightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(CCHomeTableViewController.tapFriendFocusItem))
        
        return loginRightBarButtonItem
    }()
    
    func tapFriendFocusItem() {
        print("tapFriendFocusItem")
    }
    
    private lazy var loginRightBarButtonItem: UIBarButtonItem = {
        let loginRightBarButtonItem = UIBarButtonItem(title: "登陆", style: .Plain, target: self, action: #selector(CCHomeTableViewController.tapLoginItem))
        
        return loginRightBarButtonItem
    }()
    
    func tapLoginItem() {
        print("tapLoginItem")
    }
    
    private lazy var radarOrScanRightBarButtonItem: UIBarButtonItem = {
        let loginRightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(CCHomeTableViewController.tapRadarOrScanItem))
        
        return loginRightBarButtonItem
    }()
    
    func tapRadarOrScanItem() {
        print("tapRadarOrScanItem")
    }
    
    private lazy var popoverAnimation: CCPopoverAnimation = {
        let popoverAnimation = CCPopoverAnimation()
        let x = self.titleView.center.x - 100
        let y = CGRectGetMaxY((self.navigationController?.navigationBar.frame)!) - 10
        popoverAnimation.popViewFrame = CGRect(x: x, y: y, width: 200, height: 250)
        
        return popoverAnimation
    }()
}

extension CCHomeTableViewController: CCPopViewControllerDelegate {
    func popViewControllerHide(popViewController: CCPopViewController) {
        titleView.selected = false
    }
    
    func popViewControllerShow(popViewController: CCPopViewController) {
        titleView.selected = true
    }
}